//
//  LLMallDoCell.m
//  LoveLove
//
//  Created by 肖兰月 on 2019/4/6.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLMallDoCell.h"
#import "LLMallDoModel.h"

@interface LLMallDoCell ()
@property (strong, nonatomic) UIImageView *imgView;
@property (strong, nonatomic) UILabel *nicknameLbl;
@property (strong, nonatomic) UILabel *remainingTimeLbl;
@property (strong, nonatomic) UILabel *countLbl;
@property (strong, nonatomic) LLButton *doBtn;
@end

@implementation LLMallDoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews {
    CGFloat leftMargin = 12;
    _imgView = [UIImageView new];
//    _imgView.frame = CGRectMake(leftMargin, 10, 40, 40);
    _imgView.layer.cornerRadius = 20;
    _imgView.contentMode = UIViewContentModeScaleAspectFill;
    _imgView.clipsToBounds = YES;
    _imgView.backgroundColor = UIColor.darkGrayColor;
    [self.contentView addSubview:_imgView];
    
    _nicknameLbl = [LLInitBaseViewTool labelWithText:nil textColor:LLColorRGB(0) font:15 textAlignment:NSTextAlignmentLeft];
//    _nicknameLbl.frame = CGRectMake(_imgView.right+5, 20, LLScreenWidth-_imgView.right-5-70-leftMargin-100, 20);
    [self.contentView addSubview:_nicknameLbl];
    
    _doBtn = [LLInitBaseViewTool buttonWithTitle:nil image:nil titleColor:UIColor.whiteColor font:14];
    _doBtn.layer.cornerRadius = 5;
    _doBtn.backgroundColor = UIColor.redColor;
//    _doBtn.frame = CGRectMake(LLScreenWidth-leftMargin-70, 15, 70, 30);
    [_doBtn addTarget:self action:@selector(doBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_doBtn];
    
    _countLbl = [LLInitBaseViewTool labelWithText:nil textColor:nil font:0 textAlignment:NSTextAlignmentRight];
//    _countLbl.frame = CGRectMake(LLScreenWidth-100-70-leftMargin-5, 10, 100, 20);
    [self.contentView addSubview:_countLbl];
    
    _remainingTimeLbl= [LLInitBaseViewTool labelWithText:nil textColor:LLColorRGB(91) font:14 textAlignment:NSTextAlignmentRight];
//    _remainingTimeLbl.frame = CGRectMake(LLScreenWidth-100-70-leftMargin-5, _countLbl.bottom, 100, 20);
    [self.contentView addSubview:_remainingTimeLbl];
}

- (void)setModel:(LLMallDoModel *)model {
    _model = model;
    [_imgView sd_setImageWithURL:LLImageWithUrl(model.imgUrl) placeholderImage:[UIImage imageNamed:@""] options:SDWebImageRefreshCached];
    _nicknameLbl.text = model.nickname;
    NSString *countStr = [NSString stringWithFormat:@"还差%@人拼成",model.countStr];
    _countLbl.attributedText = [NSAttributedString attributedStringsWithText:countStr textFont:14 textRange:NSMakeRange(2, model.countStr.length+1) textColor:LLColor(225, 0, 0) isBoldFont:NO];
    _remainingTimeLbl.text = [NSString stringWithFormat:@"剩余%@",model.remainingTime];
    [_doBtn setTitle:model.doText forState:0];
}

- (void)doBtnClick {
    if ([self.delegate respondsToSelector:@selector(cellDidClickDo:)]) {
        [self.delegate cellDidClickDo:self];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    if (self.type == LLMallDoCellTypeOther) {
        CGFloat leftMargin = 12;
        _imgView.frame = CGRectMake(leftMargin, 10, 40, 40);
        _nicknameLbl.frame = CGRectMake(_imgView.right+5, 20, self.width-_imgView.right-5-70-leftMargin-100, 20);
        _doBtn.frame = CGRectMake(self.width-leftMargin-70, 15, 70, 30);
        _countLbl.frame = CGRectMake(self.width-100-70-leftMargin-5, 10, 100, 20);
        _remainingTimeLbl.frame = CGRectMake(self.width-100-70-leftMargin-5, _countLbl.bottom, 100, 20);
    } else {
        CGFloat leftMargin = 12;
        _imgView.frame = CGRectMake(leftMargin, 10, 40, 40);
        _nicknameLbl.frame = CGRectMake(_imgView.right+5, _imgView.top, self.width-_imgView.right-5-70-leftMargin-80, 20);
        _doBtn.frame = CGRectMake(self.width-leftMargin-70, 15, 70, 30);
        _countLbl.frame = CGRectMake(self.width-80-70-leftMargin-5, 10, 80, 20);
        _remainingTimeLbl.frame = CGRectMake(_nicknameLbl.left, _imgView.bottom-20, 100, 20);
        _remainingTimeLbl.textAlignment = NSTextAlignmentLeft;
    }
}

@end
